package com.jspgou.cms.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.cms.dao.PromoteDao;
import com.jspgou.cms.entity.Promote;
import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;

@Repository
public class PromoteDaoImpl extends HibernateBaseDao<Promote, Long> implements PromoteDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long websiteId,Long storeId,Boolean disable,
			Boolean deleted,int pageNo, int pageSize){
		Finder f=Finder.create("from Promote bean where 1=1");
		if(websiteId!=null){
			f.append(" and bean.website.id=:websiteId");
			f.setParam("websiteId", websiteId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		if(disable!=null){
			f.append(" and bean.disable=:disable");
			f.setParam("disable", disable);
		}
		if(deleted!=null){
			f.append(" and bean.deleted=:deleted");
			f.setParam("deleted", deleted);
		}
		return find(f,pageNo,pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<Promote> getList(Long websiteId,Long storeId,Boolean disable,Boolean deleted){
		Finder f=Finder.create("from Promote bean where 1=1");
		if(websiteId!=null){
			f.append(" and bean.website.id=:websiteId");
			f.setParam("websiteId", websiteId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		if(disable!=null){
			f.append(" and bean.disable=:disable");
			f.setParam("disable", disable);
		}
		if(deleted!=null){
			f.append(" and bean.deleted=:deleted");
			f.setParam("deleted", deleted);
		}
		return find(f);
	}

	public Promote findById(Long id) {
		Promote entity = get(id);
		return entity;
	}

	public Promote save(Promote bean) {
		getSession().save(bean);
		return bean;
	}

	public Promote deleteById(Long id) {
		Promote entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<Promote> getEntityClass() {
		return Promote.class;
	}
	
	@SuppressWarnings("unchecked")
	public boolean checkingProduct(Long id) {
		Date now = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String CurDate = df.format(now); 
		String sql = "select b.product_id,a.startDate,a.endDate from jc_promote a,jc_shop_product b where a.promote_id = b.promote_id and b.product_id='"+id+"'";
		List<Object[]> list = new ArrayList<Object[]>();
		list = getSession().createSQLQuery(sql).list();
		if(list.size()>0){
			Object[] obj = list.get(0);
			String StartDate = df.format(obj[1]);
			String EndDate = df.format(obj[2]);
			int i = CurDate.compareTo(StartDate);
			int j = CurDate.compareTo(EndDate);
			if(i>0 && j<0){
				return true;
			}else{
				return false;
			}
		}
		else{
			return false;
		}
	}
}